package com.scy.java.object.StringDemo;

public class StringBufferDemo {
    public static void main(String[] args) {
        //1.创建StringBuffer对象
        //定义一个空字符串缓冲区，默认容量为16个字符
        StringBuffer stringBuffer = new StringBuffer();
        //定义一个含有10个字符容量的字符串缓冲区
        StringBuffer stringBuffer1 = new StringBuffer(10);
        //定义一个含有16+4个字符容量的字符串缓冲区
        StringBuffer stringBuffer2 = new StringBuffer("青春无悔");
//         System.out.println(stringBuffer2.capacity());//20
        //2.追加字符串
        //创建StringBuffer对象
        StringBuffer sbf = new StringBuffer("Hello");
        String str = "World";
        //StringBuffer追加str字符串
        sbf.append(str);
//        System.out.println(sbf);//HelloWorld
        //3.替换字符
//       StringBuffer类setCharAt(int index,char ch) 方法 索引从0开始
        sbf.setCharAt(2,'L');
        System.out.println(sbf);//HeLloWorld
        sbf.setCharAt(6,'O');
        System.out.println(sbf);//HeLloWOrld
        //反转字符串
        //StringBuffer类中的reverse()方法
        //用于将字符串列用其反转的形式取代
        StringBuffer stringBuffer3 = new StringBuffer("Java");
        System.out.println(stringBuffer3.reverse());//avaJ
        //删除字符串
        //5.删除字符申
        //StringBuffer类提供TdeLeteCharAt()和delete()
        // 两个删除字符串的方法
        // 5.1 deLeteCharAt(int index)用于移除序列中指定位置的字符
        StringBuffer stringBuffer4 = new StringBuffer("world");
        System.out.println(stringBuffer4.deleteCharAt(2));//wold
        //5.2 deLete(int start,int end)用于移除序列中区间子字符串的字符串
        StringBuffer stringBuffer5 = new StringBuffer("hello HaoLp");
        //将字符串"hello HaoLp"之间的2-8位置删除（含头不含尾）
        stringBuffer5.delete(2,8);
        System.out.println(stringBuffer5);//heLp


    }
}
